var ajax = require('../../utils/ajax.js');
var common = require('../../utils/common.js');
var app = getApp();
var curPage;
var curScope;
var _onSearch;
var _showType = 0;

function init({
  page,
  scope,
  cmpt
}) {
  curPage = page;
  curScope = scope || 'nearShopData';

  _showType = cmpt['content']['layout']['showType'] || 0;
  _getLoaction();

  // _setData({
  //   lng: '',
  //   lat: ''
  // });

  curPage['nearShop.markertap'] = _markertap;
}

function _markertap() {
  console.log('---1234');
}

/**
 * 获取gps定位
 */
function _getLoaction() {
  var nearShopData = _getData();
  var {
    lat,
    lng
  } = app.getLocation();

  nearShopData.locating = true;
  nearShopData.showType = _showType;
  nearShopData.lat = lat;
  nearShopData.lng = lng;
  _setData(nearShopData);

  // if (!lat || !lng) {
  wx.getLocation({
    success: function(result) {
      var lat = result.latitude;
      var lng = result.longitude;

      app.setLocation(lat, lng);
      _getNearShop();

      nearShopData.gpsFail = false;
      nearShopData.lat = lat;
      nearShopData.lng = lng;
      _setData(nearShopData)
    },
    fail: function(result) {
      nearShopData.gpsFail = true;
      _setData(nearShopData)
    }
  });
  // } else {
  //   _getNearShop();
  // }
}

/**
 * 重新授权共享地址
 */
function _getGPS(evt) {
  var formId = evt.detail.formId;

  wx.openSetting({
    success: function(res) {
      _getLoaction();
    }
  })
  common.submitForm(formId);
}

/**
 * 获取最近的店
 */
function _getNearShop() {
  var nearShopData = _getData();
  var {
    lat,
    lng
  } = app.getLocation();

  ajax.G('/openapi/CHome/nearbyStoresList', {
    page: 1,
    lng,
    lat,
  }, function(data) {
    var nearList = data.result['nearbyStoresList'] || [];

    if (!_showType || _showType == 0) {
      if (nearList.length > 0) {
        var nearShop = nearList[0];

        if (nearShop.juli <= 100) {
          nearShop.juli = '100米以内';
        } else if (nearShop.juli <= 1000) {
          nearShop.juli = nearShop.juli + ' m';
        } else {
          nearShop.juli = (parseFloat(nearShop.juli) / 1000).toFixed(2) + ' km';
        }
        nearShopData['nearShop'] = nearShop;
      }
    } else {
      let polyline = {
        points: [{
          latitude: lat,
          longitude: lng
        }],
        color: '#1AAD16ff',
        width: 6,
        arrowLine: true
      };
      let includePoints = [{
        latitude: lat,
        longitude: lng
      }]


      nearShopData['markers'] = nearList.map(function(item, index) {
        if (index == 0) {
          polyline['points'].push({
            latitude: item.lat,
            longitude: item.lng
          });
        }
        includePoints.push({
          latitude: item.lat,
          longitude: item.lng
        });
        return {
          id: item.id,
          latitude: item.lat,
          longitude: item.lng,
          title: item.title,
          iconPath: "/images/redaddr.png",
          width: 45,
          height: 45,
        };
      });
      nearShopData.polyline = [polyline];
      nearShopData.includePoints = includePoints;
    }

    nearShopData.locating = false;
    _setData(nearShopData);
  });
}


function _setData(data) {
  var obj = _getData();

  Object.assign(obj, data);

  console.log(obj)
  curPage.setData({
    [`${curScope}`]: obj
  });
}

function _getData(key) {
  var data = curPage.data[`${curScope}`] || {};

  if (!key) {
    return data;
  } else {
    return data[key];
  }
}

module.exports = {
  init: init,
};